﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ignitron.EEngine.Maths;
using GameEngine;

namespace Board3D.Animation
{
    internal delegate void AnimationDelegate(Position figure, EVector3 pos, float alpha);

    internal class AnimationBlock
    {
        List<Animation> _animations;

        public AnimationBlock()
        {
            _animations = new List<Animation>();
            Finished = false;
        }

        public bool Finished
        { get; set; }

        public event AnimationDelegate AnimationEvent;

        public void AddAnimation(Animation anim)
        {
            _animations.Add(anim);
        }

        public void StartAnimation()
        {
            if (_animations.Count > 0)
                _animations[0].StartAnimation();
        }

        public bool UpdateAnimation()
        {
            if (_animations.Count == 0)
            {
                Finished = true;
                return false;
            }

            Animation anim = _animations[0];
            //if (anim is Slide2DMoveAnimation)
            //{
            //    if (AnimationEvent != null)
            //        AnimationEvent(anim.GetFigurePos(), Vector3.Zero, 0, (anim as Slide2DMoveAnimation).GetCurrentPoint());
            //}
            if (anim is MoveAnimation)
            {
                if (AnimationEvent != null)
                    AnimationEvent(anim.GetFigurePos(), anim.GetFigureAnimationPos(), 0);
            }
            else
            {
                if (AnimationEvent != null)
                    AnimationEvent(anim.GetFigurePos(), EVector3.Zero, anim.GetFigureAnimationAlpha());
            }

            if (anim.Finished == true)
            {
                _animations.RemoveAt(0);
                StartAnimation();
            }

            return true;
        }

    }
}
